<template>
    <div>
        <Tables 
        :title ='title' 
        :butns = 'butns' 
        :field = 'field' 
        :tableData ='tableData' 
        :pageSize='pageSize' 
        :totalNum="total_num" 
        :collectField = "collect_field"
        :curPage ="cur_page" 
        :clearSelectItems="selectedItems"
        @reset="reset"  
        @loadData ="loadData" 
        @sentSelectedItems="getSelectedItems"
        @sentEditData="getEditData"
        @changePageSize = 'changePageSize'
        @rankData = "rankData"
        ></Tables>

        <Modals 
        :id ="'modal'" 
        :title ='title' 
        :field = 'field' 
        :collectField = "collect_field"
        :selectItem = "select_item" 
        :status = "status" 
        :errorMsg="errorMsg" 
        :type="type"
        :selectedItems="selectedItems" 
        :editData = "edit_data"  
        @sentErrorMsg = "getErrorMsg"
        @addEvent = 'addData'
        @delEvent = 'delData'
        @editEvent = 'editData'
        ></Modals>
    </div>
</template>
<script>
    import Tables from 'components/common/Tables'
    import Modals from 'components/common/Modals'
    import defendService from 'services/defendService' 
    import PageData from 'libs/PageData'

    export default {
		components:{
		    Tables,
            Modals        
	    },
        data() {
            return{
                title:this.$t('safeplot.navBtn11'),
                butns: [
                    {
                        type:'add',
                        name:this.$t('common.addBtn'),
                        event:'addEvent',
                        icon:'/static/img/add.png',
                        class:'add_btn',
                    },
                    {
                        type:'del',
                        name:this.$t('common.delBtn'),
                        event:'delEvent',
                        icon:'/static/img/delete.png',
                        class:'del_btn',
                    },
                    {
                        type:'edit',
                        name:this.$t('common.editBtn'),
                        event:'editEvent',
                        icon:'/static/img/modpic.png',
                        class:'edit_btn',
                    }
                ],
                field: [
                    {
                        type:'checkbox', 
                    },
                    {
                        type:'rank'
                    },
                    {
                        type:'text',
                        label:this.$t('safeplot.fakewTableTh1'),
                        name:'t_ip_mac',
                        regex:/(^[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}$)|(^(!*(2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)(\/(3[0-2]|2[0-9]|1[0-9]|[1-9]))*(\-(!*(2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?))*(\/(3[0-2]|2[0-9]|1[0-9]|[1-9]))*$)/,
                        requried:true, 
                        remark:this.$t('safeplot.fakewRemark1')
                    },
                    {
                        type:'textarea',
                        label:this.$t('safeplot.fakewTableTh2'),
                        name:'t_port',
                        regex:/^((([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))(\:([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])){0,1})(\,(([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))(\:([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])){0,1}){0,9}$/,
                        requried:true, 
                        remark:this.$t('safeplot.fakewRemark2')   
                    },
                ],
                tableData:[],
                pageSize: [10, 20, 40, 80],
                size:10,
                total_num:1,
                cur_page:1,
                collect_field:'id',              
                select_item:[],
                status:"",
                errorMsg:'',
                type:'',
                selectedItems:[],
                edit_data:{},
                order: '',
                by: ''
            }
        },
        created() {
            this.loadData()
        },
        methods:{
            getSelectedItems(selectedItems) {
                this.selectedItems = selectedItems
            },
            getEditData(editdata) {
                this.edit_data = editdata 
            },
            getErrorMsg(msg) {
                this.errorMsg = msg
            },
            reset(type) {
                this.status = ""
                this.errorMsg = ''
                this.type = type
                $('.inputs_wrap .error_foramt').removeClass('error_foramt')
            },  
            loadData() {
                this.status ='ing'
                let params = {
                    page: this.cur_page,
                    row: this.size,
                    order: this.order,
                    by: this.by
                }
                defendService.getSafePolt(9, params)
                .then((res) => {
                    
                    if (res.errcode === 0) {
                        this.status ='ok'
                        this.total_num = res['9'].count				
                        this.tableData =  res['9'].data
                    } else {
                        this.status = 'error'
                        this.errorMsg = res.errorMessage	
                    }
                })
            },
            changePageSize (nowpage, size) {
                this.cur_page = nowpage
                this.size = size
                this.loadData()
            },
            rankData (param) {
                this.order = param.order
                this.by = param.by
                this.loadData()
            },
            addData(params) {
                this.status ='ing'
                let sendStr = `${params.t_ip_mac}|${params.t_port}`
                defendService.addSafePolt(9, {9: sendStr})
                .then((res) => {	
                    if (res.errcode === 0) {
                        this.status ='ok'
                        this.selectedItems =[]
                        this.loadData()
                    } else {
                        this.status ='error'
                        this.errorMsg = res.errorMessage
                    }
				})
            },
            delData(params) {
                this.status ='ing'
                defendService.delSafePolt(9, {9: params.ids})
                .then((res) => {
					
                    if (res.errcode === 0) {
                        this.status ='ok'
                        this.selectedItems =[]
                        this.loadData()
                    } else {
                        this.status ='error'
                        this.errorMsg = res.errorMessage
                    }
				})
            },
            editData (params) {
                let sendStr = `${params.id}|${params.t_ip_mac}|${params.t_port}`
                this.status ='ing'
                defendService.editSafePolt(9, {9: sendStr})
                .then((res) => {
                    if (res.errcode === 0) {
                        this.status ='ok'
                        this.loadData()
                    } else {
                        this.status ='error'
                        this.errorMsg = res.errorMessage
                    }
                })
            },
        }
    }
</script>